package com.kmzx.one.project.service.impl;

import com.kmzx.one.common.exceptions.NoSuchObjectException;
import com.kmzx.one.common.exceptions.ParameterNullException;
import com.kmzx.one.common.utils.UUIDGenerator;
import com.kmzx.one.project.entity.UserAddress;
import com.kmzx.one.project.mapper.ChargesMapper;
import com.kmzx.one.project.mapper.UserAddressMapper;
import com.kmzx.one.project.others.dto.UserAddressInfo;
import com.kmzx.one.project.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;


    @Override
    public List<UserAddressInfo> findAllAddress(String userId) throws Exception {
        List<UserAddressInfo> list = userAddressMapper.findAllAddress(userId);
        if (list != null && list.size() > 0) {
            return list;
        } else {
            List<UserAddressInfo> objects = Collections.emptyList();
            return objects;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer userAddressAdd(UserAddressInfo info) throws Exception {
        UserAddress userAddress = new UserAddress();
        Date now = new Date();
        userAddress.setId(UUIDGenerator.generate());
        userAddress.setProvince(info.getProvince());
        userAddress.setCity(info.getCity());
        userAddress.setTown(info.getTown());
        userAddress.setOthers(info.getOthers());
        userAddress.setMobile(info.getMobile());
        userAddress.setUserId(info.getUserId());
        userAddress.setAddressDefault(info.getIsDefault());
        userAddress.setCreateTime(now);
        userAddress.setUpdateTime(now);
        userAddress.setDeleted(0);
        userAddress.setPostal(info.getPostal());
        userAddress.setUserName(info.getUserName());
        userAddress.setCode(info.getCode());

        int isDefault = info.getIsDefault();
        if (isDefault == 1) {
            userAddressMapper.resetUserDefaultAddress(info.getUserId());
        }
        int insert = userAddressMapper.insertAddress(userAddress);
        return insert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer userAddressEdit(UserAddressInfo info) throws Exception {
        UserAddress userAddress = userAddressMapper.selectById(info.getAddressId());
        if (userAddress == null) {
            throw new ParameterNullException("地址不存在");
        }
        Date now = new Date();
        int isDefault = info.getIsDefault();
        userAddress.setProvince(info.getProvince());
        userAddress.setCity(info.getCity());
        userAddress.setTown(info.getTown());
        userAddress.setMobile(info.getMobile());
        userAddress.setOthers(info.getOthers());
        userAddress.setUserId(info.getUserId());
        userAddress.setUserName(info.getUserName());
        userAddress.setPostal(info.getPostal());
        userAddress.setAddressDefault(isDefault);
        userAddress.setUpdateTime(now);
        userAddress.setCode(info.getCode());
        if (isDefault == 1) {
            userAddressMapper.resetUserDefaultAddress(info.getUserId());
        }
        int insert = userAddressMapper.updateByIdSelective(userAddress);
        return insert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delUserAddress(String userId, String addressId) throws Exception {
        UserAddress userAddress = userAddressMapper.selectById(addressId);
        if (userAddress == null) {
            throw new ParameterNullException("地址不存在");
        }
        Date now = new Date();
        userAddress.setDeleted(1);
        userAddress.setUpdateTime(now);
        int insert = userAddressMapper.updateByIdSelective(userAddress);
        return insert;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer setUserAddressDefault(String userId, String addressId) throws Exception {
        UserAddress userAddress = userAddressMapper.selectById(addressId);
        if (userAddress == null) {
            throw new ParameterNullException("该地址不存在");
        }
        if (userAddress.getDeleted() == 1) {
            throw new NoSuchObjectException("该地址不存在");
        }
        Integer addressDefault = userAddress.getAddressDefault();
        if (addressDefault == 0) {
            userAddressMapper.resetUserDefaultAddress(userId);
        }
        userAddress.setAddressDefault(1);
        int insert = userAddressMapper.updateByIdSelective(userAddress);
        return insert;
    }
}
